package com.quanyan.orderpay.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.enums.PlatformTypeEnum;
import com.quanyan.orderpay.service.PayWaySettingService;
import com.quanyan.orderpay.vo.request.RespPayWaySettingVo;
import com.quanyan.pay.entity.TbPayWaySetting;
import com.quanyan.pay.entity.TbPayWaySettingExample;
import com.quanyan.pay.mapper.TbPayWaySettingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zheng.Ke
 * Date 2016/11/7.
 */
@Component("adminPayWaySettingService")
public class PayWaySettingServiceImpl implements PayWaySettingService {
    @Autowired
    private TbPayWaySettingMapper tbPayWaySettingMapper;


    @Override
    public APIResponse<List<RespPayWaySettingVo>> payWayList() {
        List<RespPayWaySettingVo> payWayList = new ArrayList<RespPayWaySettingVo>();
        String[] platforms = {PlatformTypeEnum.IOS.getCode(), PlatformTypeEnum.ANDROID.getCode(), PlatformTypeEnum.H5.getCode()};
        for (String platform : platforms) {
            String platformName = "";
            RespPayWaySettingVo setting = new RespPayWaySettingVo();
            if (PlatformTypeEnum.IOS.getCode().equals(platform)) {
                platformName = platform + "客户端";
            } else if (PlatformTypeEnum.ANDROID.getCode().equals(platform)) {
                platformName = platform + "客户端";
            } else if (PlatformTypeEnum.H5.getCode().equals(platform)) {
                platformName = platform;
            }
            setting.setPlatform(platform);
            setting.setPlatformName(platformName);
            setting.setPayWayList(this.findByPlatform(platform));
            payWayList.add(setting);
        }
        return APIResponse.returnSuccess(payWayList);
    }

    @Override
    public List<TbPayWaySetting> findByPlatform(String platform) {
        TbPayWaySettingExample example = new TbPayWaySettingExample();
        TbPayWaySettingExample.Criteria criteria = example.createCriteria();
        criteria.andPlatformEqualTo(platform);
        return tbPayWaySettingMapper.selectByExample(example);
    }

    @Override
    public APIResponse changeSetting(int payId, String platform, byte enabled) {
        TbPayWaySetting tbPayWaySetting = getByPayIdAndPlatform(payId, platform);
        if (tbPayWaySetting == null) {
            return APIResponse.returnFail("payId或platform不正确");
        }
        tbPayWaySetting.setEnabled(enabled);
        tbPayWaySettingMapper.updateByPrimaryKey(tbPayWaySetting);

        return APIResponse.returnSuccess();
    }

    @Override
    public TbPayWaySetting getByPayIdAndPlatform(int payId, String platform) {
        TbPayWaySettingExample example = new TbPayWaySettingExample();
        TbPayWaySettingExample.Criteria criteria = example.createCriteria();
        criteria.andPlatformEqualTo(platform);
        criteria.andPayIdEqualTo(payId);
        List<TbPayWaySetting> settingList = tbPayWaySettingMapper.selectByExample(example);
        if (settingList.size() <= 0) {
            return null;
        }
        return settingList.get(0);
    }
}
